package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.bean.MyIncomeBean;
import com.sskj.cndragon.presenter.SpreadRecordActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.NormalRefreshDataSource;
import com.sskj.lib.util.HideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;

public class SpreadRecordActivity extends BaseActivity<SpreadRecordActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private String type;
    private String typeID;

    private SlimAdapter<MyIncomeBean> slimAdapter;
    private MVCCoolHelper<List<MyIncomeBean>> mvcHelper;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_spread_record;
    }

    @Override
    public SpreadRecordActivityPresenter getPresenter() {
        return new SpreadRecordActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        type = getIntent().getStringExtra(Constans.SPREAD_TYPE);
        typeID = getIntent().getStringExtra(Constans.SPREAD_TYPE_ID);
        toolbar.setTitle(type);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.item_spread_record, new SlimInjector<MyIncomeBean>() {
            @Override
            public void onInject(MyIncomeBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_mobile, CheckUtil.isMobile(data.getTel())?HideUtil.getPhoneHide(data.getTel()):data.getTel())
                        .text(R.id.tv_money, data.getUserReturnFee() + "FHB")
                        .text(R.id.tv_date, TimeFormatUtil.formatE(data.getCreateTime()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        NormalRefreshDataSource<List<MyIncomeBean>> normalRefreshDataSource = new NormalRefreshDataSource<List<MyIncomeBean>>(coolRefreshView) {
            @Override
            public Flowable<List<MyIncomeBean>> loadData() {
                return mPresenter.getMyIncomeRecord(SPUtil.get(SPConfig.ID, ""), typeID);
            }
        };
        mvcHelper.setDataSource(normalRefreshDataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.refresh();

    }

    public static void start(Context context, String type, String typeID) {
        Intent intent = new Intent(context, SpreadRecordActivity.class);
        intent.putExtra(Constans.SPREAD_TYPE, type);
        intent.putExtra(Constans.SPREAD_TYPE_ID, typeID);
        context.startActivity(intent);
    }
}
